package com.richapp.launcher.receiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.jack.netty.log.LoggerManager;
import com.jack.util.date.DateUtil;
import com.richapp.launcher.busevent.EventTimeTick;
import com.richapp.launcher.ui.activities.ScreenSaverActivity;
import com.richapp.launcher.utils.ServiceUtil;
import com.richapp.launcher.utils.Tools;

import de.greenrobot.event.EventBus;

/**
 * Created by jack on 2015/5/13 0013.
 */
public class TimeTickReceiver extends BroadcastReceiver{

    private Context context = null;

    public static final String LAST_RESTART_KEY = "lastRestart";

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        if(Intent.ACTION_TIME_TICK.equals(intent.getAction())){

            LoggerManager.getInstance().getiLogger().info(getClass(),"timeTicker");

            //系统自我维护检测重启
            timerRestart(context);

            //导入数据
            Tools.importDatabaseRawFile(context);

            //启动服务
            ServiceUtil.startService(context);

            //启动应用
            //Intent app = context.getPackageManager().getLaunchIntentForPackage("com.richapp.luancher");
            //context.startActivity(app);

            //测试屏保
            //testScreenSaver();

            //HdpKit.getinstance().addChannel();
            EventTimeTick eventTimeTick = new EventTimeTick();
            EventBus.getDefault().post(eventTimeTick);
        }
    }

    private void testScreenSaver(){
        Intent screenActivity = new Intent(context, ScreenSaverActivity.class);
        screenActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(screenActivity);
    }


    /**
     * 定时重启
     * yyyyMMddHHmmss
     */
    private void timerRestart(Context context){
        try{
            String timeStr = DateUtil.getSDate();
            long currentTime = Long.parseLong(timeStr);
            int year = Integer.parseInt(timeStr.substring(0, 4));
            int month = Integer.parseInt(timeStr.substring(4, 6));
            int day = Integer.parseInt(timeStr.substring(6, 8));
            int hour = Integer.parseInt(timeStr.substring(8,10));
            int minute = Integer.parseInt(timeStr.substring(10,12));
            int second = Integer.parseInt(timeStr.substring(12,14));


            SharedPreferences sharedPreferences = context.getSharedPreferences(LAST_RESTART_KEY, Activity.MODE_PRIVATE);

            long lastRestartTime = sharedPreferences.getLong("time", 0L);
            if(currentTime - lastRestartTime > 60*30){
                if(hour == 03 && minute == 30 && day%7==0){

                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putLong("time",currentTime);
                    editor.commit();

                    Tools.restartSystem(context);
                }
            }
        }catch (Exception e){
            LoggerManager.getInstance().getiLogger().error(getClass(),e.getMessage());
        }
    }
}
